#!/usr/bin/env python
#-*- coding: UTF-8 -*-  

import cmd

import sys

reload(sys)
sys.setdefaultencoding('utf8')

from lib.core import *

username = ''
password = ''
VERSION = '0.0.1'

class CLI(cmd.Cmd):
    '''command line'''
    def __init__(self):
        cmd.Cmd.__init__(self)
        self.prompt = "diceboy>"

    def default(self, line):
        print line, u'不支持的命令'

    def do_friend(self, arg):
        theurl = url_list['friend']
        result = digu.timeline(theurl) 
        if result:
            digu.current_list = parse_timeline(result)
            display_timeline(digu.current_list)
        else:
            print errorcode_list[parse_errorcode(digu.errorcode)]

    def do_public(self, arg):
        theurl = url_list['public']
        result = digu.timeline(theurl) 
        if result:
            digu.current_list = parse_timeline(result)
            display_timeline(digu.current_list)
        else:
            print errorcode_list[parse_errorcode(digu.errorcode)]
        
    def do_user(self, user = None):
        theurl = url_list['user']
        result = digu.timeline(theurl) 
        if result:
            digu.current_list = parse_timeline(result)
            display_timeline(digu.current_list)
        else:
            print errorcode_list[parse_errorcode(digu.errorcode)]
    
    def do_replies(self, arg):
        theurl = url_list['replies']
        result = digu.timeline(theurl) 
        if result:
            digu.current_list = parse_timeline(result)
            display_timeline(digu.current_list)
        else:
            print errorcode_list[parse_errorcode(digu.errorcode)]
       
    def do_favorites(self, user = None):
        theurl = url_list['favorites']
        result = digu.timeline(theurl) 
        if result:
            digu.current_list = parse_timeline(result)
            display_timeline(digu.current_list)
        else:
            print errorcode_list[parse_errorcode(digu.errorcode)]

    def do_dialog(self, index = None):
        index = input_index(index,len(digu.current_list), u'请指定要查看消息的序号：')
        if is_reply(digu.current_list[index - 1]['in_reply_to_status_id']):
            theurl = url_list['show']
            tmp_list = []
            tmp_list.append(digu.current_list[index - 1])
            digu.current_list = tmp_list
            while is_reply(digu.current_list[-1]['in_reply_to_status_id']):
                digu_id = digu.current_list[-1]['in_reply_to_status_id']
                result = digu.timeline(theurl, digu_id = digu_id)
                if result :
                    xmldoc = minidom.parseString(result)
                    status = xmldoc.firstChild
                    digu.current_list.append(parse_status(status))
                else:
                    print errorcode_list[parse_errorcode(digu.errorcode)]
                    
                    #这里异常发生时，会退出整个程序。
                    sys.exit(1)
                    
            display_timeline(digu.current_list)

        else:
            print '此消息不是一条回复消息。'

    def do_update(self, msg = None):
        try:
            msg = input_msg(msg, u'输入你要发送的内容：')
        except EOFError, e:
            print
        else:
            if digu.update(msg):
                print u'发送成功'
            else:
                print errorcode_list[parse_errorcode(digu.errorcode)]

    def do_reply(self, index = None):
        index = input_index(index, len(digu.current_list), u'指定要回复的嘀咕消息：')

        try:
            msg = input_msg(None, u'你要回复的内容：')
        except EOFError, e:
            print
        else:
    
            digu_id = digu.current_list[index - 1]['id']
            
            if digu.update(msg, digu_id):
                print u'回复成功'
            else:
                print errorcode_list[parse_errorcode(digu.errorcode)]
    
    def do_destroy(self, index = None):
        index = input_index(index, len(digu.current_list), u'指定要删除的嘀咕消息：')
        digu_id = digu.current_list[index - 1]['id']
        theurl = url_list['destroy']
        if digu.timeline(theurl, digu_id = digu_id):
            print u'删除成功'
        else:
            print errorcode_list[parse_errorcode(digu.errorcode)]

    def do_test(self, arg):
        theurl = url_list['friend']
        
        print digu.timeline(theurl)

    def help_quit(self):
        print u'usage: quit\nquit.'

    def do_quit(self, arg):
        sys.exit(0)

    def do_EOF(self, arg):
        print
        sys.exit(0)

    def help_version(self, arg):
        print u'usage: version\nveiw the version.'

    def do_version(self, arg):
        print 'diceboy:' + VERSION

if __name__ == '__main__':
    if len(sys.argv) == 1:
        '''Interactive'''
        cli = CLI()
        digu = Digu(username, password)
        welcomeinfo = u'welcome to diceboy, a DIgu CliEnt Base On pYthon.'
        cli.cmdloop(welcomeinfo)
